package meijuli.com.join51.business.ui;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.view.View;

/**
 * 仅仅作为下拉刷新头控件使用
 *
 * @author Scott Smith  @Date 2016年04月16/4/22日 16:30
 * @Note 标注级别: Temp
 */
public class RingView extends View {
    private Paint mPaint;
    private int mBackColor;
    private int mRingColor;
    private int mRingWidth;
    private RectF mRectF;
    private float mPercent;

    public RingView(Context context, int backColor, int ringColor, int ringWidth) {
        super(context);
        this.mBackColor = backColor;
        this.mRingColor = ringColor;
        this.mRingWidth = ringWidth;

        mRectF = new RectF();
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStrokeWidth(mRingWidth);
        mPaint.setStyle(Paint.Style.STROKE);
    }

    public void setPercent(float percent) {
        mPercent = percent;
        invalidate();
    }

    public void reset() {
        mPercent = 0.0f;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int w = getWidth();
        int h = getHeight();

        int radius = w > h ? (h - 2 * mRingWidth) / 2 : (w - 2 * mRingWidth) / 2;
        mRectF.set(mRingWidth / 2, mRingWidth / 2, 2 * radius + mRingWidth + mRingWidth / 2, 2 * radius + mRingWidth + mRingWidth / 2);

        mPaint.setColor(mBackColor);
        canvas.drawArc(mRectF, 0, 360, false, mPaint);

        mPaint.setColor(mRingColor);
        canvas.drawArc(mRectF, 0, 360 * mPercent, false, mPaint);
    }
}
